﻿/**************************************************************************
创建时间:	2020/6/28 15:29:40    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using Org.BouncyCastle.Asn1.Cms;
using StackExchange.Redis;
using System;
using ZhCun.DBModel;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils.Configs;

namespace ZhCun.WebUtils.Authorizes
{
    public class LoginStateRedis : LoginStateMemory
    {
        static LoginStateRedis()
        {
            _Redis = ConnectionMultiplexer.Connect(CommonConfig.THIS.RedisConfig);
            _RedisDB = _Redis.GetDatabase();
        }

        /// <summary>
        /// redis 对象
        /// </summary>
        private static readonly ConnectionMultiplexer _Redis;
        /// <summary>
        /// redis 数据库
        /// </summary>
        private static readonly IDatabase _RedisDB;

        /// <summary>
        /// 获取LoginToken对象
        /// </summary>
        public override LoginUser Get(string token)
        {
            if (token.IsEmpty()) return null;

            LoginUser user;
            user = base.Get(token);
            if (user != null) return user;

            var fileds = _RedisDB.HashGetAll(token);
            if (fileds == null) return null;
            user = new LoginUser
            {
                UserId = fileds[0].Value,
                UserName = fileds[1].Value,
                RoleId = fileds[2].Value,
                Expires = TimeSpan.Parse(fileds[3].Value),
                ExpiresTime = DateTime.Parse(fileds[4].Value)
            };
            MemoryCacheHelper.Set(token, user, user.Expires);
            return user;
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        public override string Set(LoginUser user)
        {
            string token = base.Set(user);
            HashEntry[] fileds = new HashEntry[5];
            fileds[0] = new HashEntry(nameof(user.UserId), user.UserId);
            fileds[1] = new HashEntry(nameof(user.UserName), user.UserName);
            fileds[2] = new HashEntry(nameof(user.RoleId), user.RoleId);
            fileds[3] = new HashEntry(nameof(user.Expires), user.Expires.ToString());
            fileds[4] = new HashEntry(nameof(user.ExpiresTime), user.ExpiresTime.ToString());
            _RedisDB.HashSet(token, fileds);
            _RedisDB.KeyExpire(token, user.Expires);
            return token;
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        public override LoginUser Remove(string token)
        {
            if (token.IsEmpty()) return null;
            var user = Get(token);
            if (user != null)
            {
                base.Remove(token);
                _RedisDB.KeyDelete(token);
            }
            return user;
        }

        public override string RefreshToken(string token)
        {
            var user = Get(token);
            if (token == null) return null;
            _RedisDB.KeyExpire(token, TimeSpan.FromMinutes(2));
            MemoryCacheHelper.Set(token, user, TimeSpan.FromMinutes(2));
            var newToken = Set(user);
            MemoryCacheHelper.Set(newToken, user, user.Expires);

            return Set(user);
        }
    }
}